package com.tsugun.mediaplayer.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.MusicModel;
import com.tsugun.mediaplayer.view.loader.MediaImageLoad;

import java.util.List;

import static com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper.*;

/**
 * 卡片列表项目适配器
 * Created by shize on 2017/4/11.
 */

public class MusicCardItemAdapter extends RecyclerView.Adapter<MusicCardItemAdapter.CardItemViewHolder> {

    private LayoutInflater mInflater;
    private List<MusicModel> mData;
    private OnMusicCardClickListener mCardClickListener;

    public interface OnMusicCardClickListener {
        void onItemClick(int position);
    }

    public MusicCardItemAdapter(Context context, List<MusicModel> data) {
        mData = data;
        mInflater = LayoutInflater.from(context);
    }

    public void setOnMusicCardClickListener(OnMusicCardClickListener cardClickListener) {
        mCardClickListener = cardClickListener;
    }

    @Override
    public CardItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CardItemViewHolder(mInflater.inflate(R.layout.item_music_you_may_like, parent, false));
    }

    @Override
    public void onBindViewHolder(final CardItemViewHolder holder, int position) {
        MusicModel music = mData.get(position);
        holder.mIvIcon.setImageResource(R.drawable.ic_music_default);
        holder.mIvIcon.setBackgroundResource(R.color.grey_200);
        MediaImageLoad.localImageLoad(music, holder.mIvIcon);
        holder.mTvTitle.setText(music.getTitle());
        holder.mTvArtist.setText(music.getArtist());
        if (mCardClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCardClickListener.onItemClick(holder.getAdapterPosition());
                }
            });
        }

        setSettingTheme(holder);
    }

    // 设置主题
    private void setSettingTheme(CardItemViewHolder holder) {
        holder.mTvTitle.setTextColor(INSTANCE.getTEXT_COLOR());
        holder.mRlBg.setBackgroundColor(INSTANCE.getBACKGROUND_COLOR());
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class CardItemViewHolder extends RecyclerView.ViewHolder {

        private RoundedImageView mIvIcon;
        private TextView mTvTitle;
        private TextView mTvArtist;
        private RelativeLayout mRlBg;

        CardItemViewHolder(View itemView) {
            super(itemView);
            mIvIcon = (RoundedImageView) itemView.findViewById(R.id.id_item_rounded_image);
            mTvTitle = (TextView) itemView.findViewById(R.id.id_item_card_title);
            mTvArtist = (TextView) itemView.findViewById(R.id.id_item_card_artist);
            mRlBg = (RelativeLayout) itemView.findViewById(R.id.id_item_card_bg);
        }
    }
}
